#include "include/View.h"
#include <QGraphicsProxyWidget>

//initialize all components for the view
//and display the game window
View::View(QWidget *parent) :
    QWidget(parent)
{
    QGraphicsPixmapItem *backgroundRect;
    QGraphicsProxyWidget *quitProxy, *gridProxy;

    scene = new QGraphicsScene;
    scoreVal = 0;

    background = new QPixmap("resources/PositionTestBase.png");
    *background = background->scaledToWidth(800);

    monster = new QPixmap("resources/monster.png");
    *monster = monster->scaled(34, 34);

    tower = new QPixmap("resources/tower.png");
    *tower = tower->scaledToWidth(70);

    quit = new QPushButton("Quit");
    quit->move(628, 516);
    quit->resize(141, 55);

    grid = new QPushButton("Grid");
    grid->resize(577, 408);
    grid->move(23, 9);

    //add all initialized components to the QGraphicsScene object
    gridProxy      = scene->addWidget(grid);
    backgroundRect = scene->addPixmap(*background);
    score          = scene->addText(QString::number(scoreVal), QFont("Times", 14, 2));
    money          = scene->addText("0", QFont("Times", 14, 2));
    health         = scene->addText("0", QFont("Times", 14, 2));
    quitProxy      = scene->addWidget(quit);

    //align the score, money, and health text items to the
    //correct locations on the scene
    score->moveBy(715, 250);
    money->moveBy(715, 315);
    health->moveBy(715, 380);

    //set the scene for the QGraphicsView object, resize,
    //and display it in the program window
    view = new QGraphicsView(scene);
    view->resize(802, 589);
    view->show();
}

//adds a monster to the grid space associated with the click coordinates
//passed into the function and prints the picture on the scene
void View::updateLocation(int x, int y)
{
    scene->removeItem(score);
    scene->removeItem(money);
    scene->removeItem(health);

    //save last coordinates in xCoord and yCoord
    xCoord = x;
    yCoord = y;

    //Algorithm to convert x and y coordinates to correct box number
    x = (x / 34) + 1;
    y = (y / 34);
    x = x + (y*17);

    score = scene->addText(QString::number(x), QFont("Times", 14, 2));
    score->moveBy(715, 250);

    money = scene->addText(QString::number(xCoord), QFont("Times", 14, 2));
    money->moveBy(715, 315);

    health = scene->addText(QString::number(yCoord), QFont("Times", 14,2));
    health->moveBy(715, 380);

    //Algorithm to print monster QPixmaps to correct location in grid
    x = xCoord / 34;
    y = yCoord / 34;

    x = (x*34)+22;
    y = (y*34)+10;

    QGraphicsPixmapItem * enemy = scene->addPixmap(*monster);
    enemy->moveBy(x, y);
}
